﻿using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using QRCodePass.Application.Dtos;
using QRCodePass.Application.Process.Dtos;
using QRCodePass.Core.Repositories;

namespace QRCodePass.Application.Process
{
    public class ProcessService : IProcessService
    {

        private readonly IRepository<Core.Models.Process, int> _courseRepository;

        public ProcessService(IRepository<Core.Models.Process, int> courseRepository)
        {
            _courseRepository = courseRepository;
        }

        public async Task<PagedResultDto<Core.Models.Process>> GetPaginatedResult(GetProcessInput input)
        {

            var query = _courseRepository.GetAll();
            
            //统计查询数据的总条数，用于分页计算总页数
            var count = query.Count();
            //根据需求进行排序，然后进行分页逻辑的计算
            query = query.OrderBy(input.Sorting).Skip((input.CurrentPage - 1) * input.MaxResultCount).Take(input.MaxResultCount);
            //将查询结果转换为List集合，加载到内存中
            var models = await query.Include(a => a.Departments).AsNoTracking().ToListAsync();

           

            var dtos = new PagedResultDto<Core.Models.Process>
            {
                TotalCount = count,
                CurrentPage = input.CurrentPage,
                MaxResultCount = input.MaxResultCount,
                Data = models,
                FilterText = input.FilterText,
                Sorting = input.Sorting
            };
            return dtos;          
        }
    }
}
